<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * CI_Exceptions extension to route errors to controller
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @category	Exceptions
 * @author		Originals by EllisLab - modified for use with SimplyPost by Pascal Kriete
 */

class SP_Exceptions extends CI_Exceptions {

	var $CI;
	
	/**
	 * Controller
	 *
	 * @access public
	 */
	function SP_Exceptions()
	{
		parent::CI_Exceptions();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Redirects if the previous request wasn't an error request
	 *
	 * @access	public
	 * @param	mixed	error information
	 */
	function _redirect($error)
	{
		$conf	=& load_class('Config');
		$uri	=& load_class('URI');
		
		// If there is an error in the base controller we'll get a loop
		if (isset ($_SESSION['error']))
		{
			echo '<h2>Fatal Base Controller Error</h2>';
			echo $_SESSION['error']['message'];
			unset($_SESSION['error']);
			exit;
		}
		
		$_SESSION['error'] = $error;
		$redirect = $conf->site_url( $uri->uri_string() );
		header("Location: ".$redirect);
	}

	// --------------------------------------------------------------------
	
	/**
	 * General Error Page
	 *
	 * @access	private
	 * @param	string	the heading
	 * @param	string	the message
	 * @param	string	the error function name
	 * @return	string
	 */
	function show_error($heading = 'Fatal Error', $message = 'Unknown Error', $template = 'error_general')
	{
		$error = array(
			'heading'	=> $heading,
			'message'	=> $message,
			'template'	=> $template
		);
		$this->_redirect($error);
	}
	
	/**
	 * Native PHP error handler
	 *
	 * @access	private
	 * @param	string	the error severity
	 * @param	string	the error string
	 * @param	string	the error filepath
	 * @param	string	the error line number
	 * @return	string
	 */
	function show_php_error($severity, $message, $filepath, $line)
	{	
		$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
	
		$filepath = str_replace("\\", "/", $filepath);
		
		// For safety reasons we do not show the full file path
		if (FALSE !== strpos($filepath, '/'))
		{
			$x = explode('/', $filepath);
			$filepath = $x[count($x)-2].'/'.end($x);
		}
		
		$error = array(
			'severity'	=> $severity,
			'message'	=> $message,
			'filepath'	=> $filepath,
			'line'		=> $line,
			'template'	=> 'error_php'
		);
		
		$this->_redirect($error);
	}
}

?>